package com.fpost.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fpost.dao.GenericDAO;
import com.fpost.domain.ArticleDictionary;

public class ArticleDictionaryDAO extends GenericDAO<ArticleDictionary> {
	
	@Override
	public Long save(ArticleDictionary articleDictionary) throws HibernateException {
		Long articleDictionaryId = null;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			articleDictionaryId = (Long) session.save(articleDictionary);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}
		return articleDictionaryId;
	}

	@Override
	public void delete(long id) throws HibernateException {
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(get(id));
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}

	}

	@Override
	public void delete(ArticleDictionary articleDictionary) throws HibernateException {
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(articleDictionary);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}

	}

	@Override
	public ArticleDictionary get(long id) throws HibernateException {
		Session session = getSessionFactory().openSession();
		ArticleDictionary articleDictionary = null;
		try {
			session.beginTransaction();
			articleDictionary = (ArticleDictionary) session.get(ArticleDictionary.class, id);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}
		return articleDictionary;
	}
	
	public List<ArticleDictionary> getArticleDictionaryByDictionaryId(long dictionaryId)  throws HibernateException {
		Session session = getSessionFactory().openSession();
		List<ArticleDictionary> articleDictionarys;
		try {
			articleDictionarys = (List<ArticleDictionary>) session.createCriteria(ArticleDictionary.class)
				.add(Restrictions.eq("dictionaryId", dictionaryId))
				.list();
		} catch (HibernateException e) {
			e.printStackTrace();
			throw e;
		}
		return articleDictionarys;
	}
	
	@Override
	public void saveOrUpdate(ArticleDictionary articleDictionary) throws HibernateException {
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.saveOrUpdate(articleDictionary);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			throw e;
		} finally {
			session.close();
		}

	}
	
	public List<ArticleDictionary> getAll() throws HibernateException {
		Session session = getSessionFactory().openSession();
		List<ArticleDictionary> articleDictionarys;
		try {
			articleDictionarys = (List<ArticleDictionary>) session.createCriteria(ArticleDictionary.class).list();
		} catch (HibernateException e) {
			e.printStackTrace();
			throw e;
		}
		return articleDictionarys;		
	}
}
